package com.zzj.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Utils {
  public static MultipartFile rename(MultipartFile file) throws IOException {
    String originalFilename = file.getOriginalFilename();
    String[] filename = originalFilename.split("\\.");
    java.io.File fileTemp = java.io.File.createTempFile(filename[0], "." + filename[1]);
    file.transferTo(fileTemp);

    fileTemp = FileUtil.rename(fileTemp, RandomUtil.randomString(10), true, true);
    InputStream inputStream = new FileInputStream(fileTemp);
    file = new MockMultipartFile(fileTemp.getName(), inputStream);
    fileTemp.deleteOnExit();
    return file;
  }
}
